package com.onecar.carplatform.jtt.server;

import com.onecar.carplatform.jtt.codec.JttMsgDecoder;
import com.onecar.carplatform.jtt.codec.JttMsgEncoder;
import com.onecar.carplatform.jtt.handler.base.JttMsgHandler;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import org.springframework.stereotype.Component;

@Component
public class JttChannelHandler extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) {
        ChannelPipeline pipeline = socketChannel.pipeline();

        // jt808协议 包头最大长度16+ 包体最大长度1023+分隔符2+转义字符最大姑且算60 = 1100
        pipeline.addLast(new DelimiterBasedFrameDecoder(1100, Unpooled.copiedBuffer(new byte[]{0x7e}),
                Unpooled.copiedBuffer(new byte[]{0x7e, 0x7e})));
        pipeline.addLast(new JttMsgDecoder());
        pipeline.addLast(new JttMsgEncoder());
        pipeline.addLast(new JttMsgHandler());
    }
}
